package com.supor.aiot.basket.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import com.supor.aiot.basket.R;
import com.supor.aiot.basket.bean.CommonIngredientBean;
import com.supor.aiot.basket.bean.IsMainIngredients;
import com.supor.aiot.basket.bean.RecipeBean;

import java.util.ArrayList;
import java.util.List;


public class IngredientDialogAdapter extends RecyclerView.Adapter<IngredientDialogAdapter.BasketDialogHolder>{
    private Context mContext;
    private List<CommonIngredientBean> mainIngredients;

    public IngredientDialogAdapter(List<CommonIngredientBean> mList, Context mContext) {
        this.mainIngredients = mList;
        this.mContext = mContext;
    }

    @NonNull
    @Override
    public IngredientDialogAdapter.BasketDialogHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(mContext).inflate(R.layout.item_ingredient_dialog,parent, false);
        return new IngredientDialogAdapter.BasketDialogHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull BasketDialogHolder holder, int position) {
        CommonIngredientBean commonIngredientBean = mainIngredients.get(position);
        if (!TextUtils.isEmpty(commonIngredientBean.getIngredientName()))holder.tvBasketTitle.setText(commonIngredientBean.getIngredientName());
        if (!TextUtils.isEmpty(commonIngredientBean.getIngredientQuantity())&&!TextUtils.isEmpty(commonIngredientBean.getQuantityUnit1()))holder.tvBasketNum.setText(mainIngredients.get(position).getIngredientQuantity()+mainIngredients.get(position).getQuantityUnit1());
        if (commonIngredientBean.getRecipes().size()>0){
            String recipe = "";
            for (int i=0;i<commonIngredientBean.getRecipes().size();i++){
                if (i==0){
                    recipe+=commonIngredientBean.getRecipes().get(i).getRecipeName();
                }else {
                    recipe+=","+commonIngredientBean.getRecipes().get(i).getRecipeName();
                }
            }
            holder.tvDegree.setText(recipe);
        }else {
            holder.tvDegree.setVisibility(View.GONE);
        }
    }



    @Override
    public int getItemCount() {
        return mainIngredients.size();
    }

    public class BasketDialogHolder extends RecyclerView.ViewHolder{
        private TextView tvBasketTitle;
        private TextView tvBasketNum;
        private TextView tvDegree;

        public BasketDialogHolder(@NonNull View itemView) {
            super(itemView);
            tvBasketTitle = itemView.findViewById(R.id.tv_ingredient_title);
            tvBasketNum = itemView.findViewById(R.id.tv_ingredient_sum);
            tvDegree = itemView.findViewById(R.id.tv_basket_name);
        }
    }


}
